package com.xusheng.cli.controller.back;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.Result;
import com.xusheng.cli.common.validation.Update;
import com.xusheng.cli.entity.frontUserModule.FrontUser;
import com.xusheng.cli.req.FrontUserQueryReq;
import com.xusheng.cli.service.FrontUserService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author lyh
 * @since 2023-04-26 15:40
 */
@RestController
@RequestMapping("/api/v1/frontUser")
public class FrontUserController {

    @Resource
    private FrontUserService frontUserService;

    /**
     * 通过条件查询用户列表
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("")
    public Result<MyPage<FrontUser>> list(FrontUserQueryReq req) {
        return Result.success(frontUserService.userList(req));
    }

    /**
     * 通过id获取信息
     *
     * @param id 编号
     * @return 返回结果
     */
    @GetMapping("/{id}")
    public Result<FrontUser> info(@PathVariable long id) {
        FrontUser user = frontUserService.getById(id);
        return Result.success(user);
    }

    /**
     * 更新用户信息
     *
     * @param user 用户信息
     * @return 返回结果
     */
    @PutMapping("")
    public Result<Object> update(@Validated(Update.class) @RequestBody FrontUser user) {
        if (StrUtil.isNotBlank(user.getPassword())) {
            user.setPassword(DigestUtil.bcrypt(user.getPassword()));
        }
        return Result.isSuccess(frontUserService.updateById(user));
    }
}
